<template>
  <div class="updata_goods">
    <el-form :model="updateForm" label-width="100px" label-position="right">
      <el-form-item label="商品名称:">
        <el-input v-model="updateForm.goodsName" placeholder="请输入商品名称"></el-input>
      </el-form-item>
      <el-form-item label="商品原价:">
        <el-input v-model="updateForm.originalPrice" placeholder="请输入商品原价">
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="销售价格:">
        <el-input v-model="updateForm.sellPrice" placeholder="请输入销售价格">
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="商品库存:">
        <el-input v-model="updateForm.stock" placeholder="请输入商品库存">
          <template slot="append">件</template>
        </el-input>
      </el-form-item>
      <el-form-item label="限购数量:">
        <el-radio-group v-model="updateForm.isLimitQuantity" style="width: 200px;">
          <p style="margin: 10px 0;">
            <el-radio :label="0">不限购</el-radio>
          </p>
          <el-radio :label="1">
            <el-input v-model="updateForm.limitQuantity" size="mini" style="width: 120px;">
              <template slot="append">个/人</template>
            </el-input>
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- <el-form-item label="有效期:">
        <el-radio-group v-model="updateForm.isPermanent" style="width: 200px;">
          <p style="margin: 10px 0;">
            <el-radio :label="0">永久</el-radio>
          </p>
          <el-radio :label="1">
            <el-input v-model="updateForm.expireDate" size="mini" style="width: 120px;">
              <template slot="append">天</template>
            </el-input>
          </el-radio>
        </el-radio-group>
      </el-form-item> -->
      <el-form-item label="商品图片:" style="display: block;">
        <div class="uploadbtn" @click="uploadBtn">
          <input type="file" @change="upload" multiple ref="file" style="display: none;" />
          <img :src="updateForm.imageList[0]" v-if="updateForm.imageList">
          <i class="el-icon-upload" v-if="updateForm.imageList == []" />
        </div>
      </el-form-item>

      <el-form-item label="商品介绍:">
        <el-input maxlength="100" v-model="updateForm.description" type="textarea" rows="4" style="width: 500px;"></el-input>
      </el-form-item>
    </el-form>

    <p style="text-align: center;">
      <el-button @click="cancle">取消</el-button>
      <el-button type="primary" @click="confirm">确定</el-button>
    </p>


  </div>
</template>

<script>
  export default {
    props: ['row'],
    data() {
      return {
        radio: 3,
        updateForm: {}
      }
    },

    mounted() {
      this.getUpdateForm(this.row.id);
    },

    methods: {
      confirm() {
        this.$axios({
          method: 'post',
          url: '/shop/goods/update',
          data: this.$qs.stringify(this.updateForm)
        }).then((res) => {
          if(res.code == 200) {
            this.$message.success('修改商品成功');
            this.$emit('closeDialog',false);
          }
        })
      },

      getUpdateForm(id) {
        this.$axios({
          method: 'get',
          url: '/shop/goods/getGoodsById',
          params: {goodsId: id}
        }).then((res) => {
          if(res.code == 200) {
            res.data.isLimitQuantity = res.data.isLimitQuantity == null ? 0 : res.data.isLimitQuantity;
            this.updateForm = res.data;
          }
        })
      },

      cancle() {
        this.$emit('closeDialog',false);
      },

      uploadBtn(){
        this.$refs.file.dispatchEvent(new MouseEvent('click'))
      },

      upload(e) {
        let files = e.target.files
        this.$nextTick(() => {
            // this.option.img = URL.createObjectURL(files[0]);
            // this.fileinfo = files[0].name;
            // this.dialogVisible = true
        })
        for (let i = 0; i < files.length; i++) {
          const isImg = files[i].type.indexOf('image') > -1
          const isLimit = files[i].size / 1024 / 1024 < 10

          if (!isImg) {
            this.$message.error('请上传图片格式!')
            return false
          }
          if (!isLimit) {
            this.$message.error('大小不能超过 10MB!')
            return false
          }
          let formData = new FormData()
          formData.append('files', files[i])

          var image = { url: null, percent: 0 }
          // this.imageList.push(image)

          this.uploadAxios(formData, image)
        }
      },

      uploadAxios(formData, image){
        this.$axios({
          url: 'https://file.zmzncs.com/upload/upload-batch',
          method: 'post',
          data: formData,
          timeout: 1000 * 60, // 请求超时时间
          onUploadProgress: (progress)=> {
            image.percent = Math.round(progress.loaded / progress.total * 100)
          }
        }).then((res) => {
          if (res.code == 200) {
            this.addForm.imageList = [];
            this.addForm.imageList.push(res.data[0].url)
            image.percent = 100
            image.url = res.data[0].url
            // this.imgs.push(image.url)
            //  组件返回值
            // var urlList = []
            // this.imgs = [];
            // this.imageList.forEach((image)=> {
            //   this.imgs.push(image.url)
            // })
            // var urls = urlList.toString()
            // this.$emit('input', urls)
          }
        }).catch(err => {})
      },
    }
  }
</script>

<style scoped>
  .el-form-item {
    display: inline-block;
  }

  .el-input {
    width: 200px;
  }

  .uploadbtn {
    border: 1px solid #aaa;
    height: 100px;
    width: 100px;
    position: relative;
  }

  .uploadbtn img {
    max-width: 100%;
    max-height: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }

  .el-icon-upload {
    font-size: 50px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }





</style>
